package cur.nature.component.minio.controller;

import cur.nature.component.minio.config.StorageClient;
import cur.nature.framework.core.exception.BizException;
import cur.nature.framework.core.log.LOG;
import cur.nature.framework.mvc.base.BaseAPI;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "Minio 服务接口")
@RestController
@RequestMapping("component/minio/v1/api")
@ConditionalOnProperty( value = {"cur.component.minio.controller.enable"})
public class Component_MinioController {

    @Operation(summary = "本地测试接口")
    @GetMapping("/test")
    public String test() throws Exception {
        LOG.info("test : " );
        return "1";
    }

    /**
     * 上传文件
     * @param file 文件
     * @param path 路径
     * @return
     */
    @Operation(summary = "上传文件")
    @PostMapping("/upload")
    public BaseAPI.SaveResult<String> uploadMinioFile(@RequestParam("file") MultipartFile file) throws BizException {

        String originalFilename = file.getOriginalFilename();
        try {
            StorageClient.upload(originalFilename, file);
        } catch (Exception e) {
            throw new BizException(e.getMessage());
        }
        BaseAPI.SaveResult result = new BaseAPI.SaveResult();
        result.setObject("1");
        return result;
    }


    /**
     * 下载文件
     * @param filename 文件名
     * @param response 响应头
     */
    @Operation(summary = "下载文件")
    @PostMapping("/download")
    public void downloadFile(@RequestParam("filename") String filename, HttpServletResponse response) throws BizException {
        try {
            StorageClient.download(filename, response);
        } catch (Exception e) {
            throw new BizException(e.getMessage());
        }
    }


    /**
     * 获取文件的预览url
     * @param filename 文件名
     * @return
     */
    @Operation(summary = "获取文件下载URL")
    @GetMapping("/getUrl")
    public BaseAPI.SaveResult<String> getFileUrl(@RequestParam("filename") String filename){
        String fileUrl = StorageClient.getFileUrl(filename);
        BaseAPI.SaveResult result = new BaseAPI.SaveResult();
        result.setObject(fileUrl);
        return result;
    }

    /**
     * 删除文件
     * @param filename 文件名
     * @return
     */
    @Operation(summary = "删除文件")
    @GetMapping("/deleteFile")
    public BaseAPI.SaveResult<String> deleteFile(@RequestParam("filename") String filename) throws Exception {
        StorageClient.removeObject(filename);
        BaseAPI.SaveResult result = new BaseAPI.SaveResult();
        result.setObject(filename);
        return result;
    }
}